package com.ruoyi.web.controller.qt;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.Cascader;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.QtUsesrCourse;
import com.ruoyi.system.service.IQtUsesrCourseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QtUser;
import com.ruoyi.system.service.IQtUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户管理Controller
 * 
 * @author zqq
 * @date 2021-11-15
 */
@RestController
@RequestMapping("/qt/user")
public class QtUserController extends BaseController
{
    @Autowired
    private IQtUserService qtUserService;
    @Autowired
    private IQtUsesrCourseService iQtUsesrCourseService;
    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('qt:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(QtUser qtUser)
    {
        startPage();
        List<QtUser> list = qtUserService.selectQtUserList(qtUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('qt:user:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QtUser qtUser)
    {
        List<QtUser> list = qtUserService.selectQtUserList(qtUser);
        ExcelUtil<QtUser> util = new ExcelUtil<QtUser>(QtUser.class);
        return util.exportExcel(list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('qt:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        return AjaxResult.success(qtUserService.selectQtUserByUserId(userId));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('qt:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QtUser qtUser)
    {
        return toAjax(qtUserService.insertQtUser(qtUser));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('qt:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QtUser qtUser)
    {
        return toAjax(qtUserService.updateQtUser(qtUser));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('qt:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(qtUserService.deleteQtUserByUserIds(userIds));
    }
    @PreAuthorize("@ss.hasPermi('qt:user:upload')")
    @Log(title = "头像上传", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException
    {
        System.out.println(file);
        String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
        return avatar;
    }
    @Log(title = "学生导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('qt:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<QtUser> util = new ExcelUtil<QtUser>(QtUser.class);
        List<QtUser> userList = util.importExcel(file.getInputStream());
        return toAjax(qtUserService.importList(userList));
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<QtUser> util = new ExcelUtil<QtUser>(QtUser.class);
        return util.importTemplateExcel("用户数据");
    }
    @PreAuthorize("@ss.hasPermi('qt:user:batchinster')")
    @Log(title = "批量添加", businessType = BusinessType.INSERT)
    @PostMapping("/batchinster")
    public AjaxResult batchinster(@RequestBody QtUsesrCourse qtUsesrCourse)
    {
        System.out.println(qtUsesrCourse);
        return toAjax(iQtUsesrCourseService.batchinster(qtUsesrCourse));
    }

}
